package rad.framework.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class YesNoConverter implements Converter {

	private static final String YES = "Yes";

	private static final String NO = "No";

	public Object getAsObject(FacesContext context, UIComponent comp,
			String value) throws ConverterException {
		// System.out.println("getAsObject: " + value);
		if (value == null || value.trim().length() == 0) {
			return Boolean.FALSE;
		}
		if (YES.equals(value)) {
			return Boolean.TRUE;
		} else {
			return Boolean.FALSE;
		}
	}

	public String getAsString(FacesContext ctx, UIComponent comp, Object object)
			throws ConverterException {
		// System.out.println("getAsString: " + object);
		if (object == null) {
			return NO;
		}
		if (object instanceof Boolean) {
			Boolean b = (Boolean) object;
			if (b) {
				return YES;
			} else {
				return NO;
			}
		}
		return NO;
	}

}
